<%@page language="java" import="java.sql.*" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
<fmt:setBundle basename="messages.gestion"/>
<%
Integer userId=(Integer)request.getSession(false).getAttribute("id");
dbBean.DBConnectionManager connMgr=(dbBean.DBConnectionManager)application.getAttribute("connMgr");
if((userId==null) || (connMgr==null)) {
  application.log("- No existe una sesion de usuario o no se ha podido obtener una conexion con la BBDD");
  response.sendRedirect("/VEGPS/html/index.html");
} else {
application.log("- Consultamos los datos de usuario en la BBDD");
Connection con = connMgr.getConnection("mysql");
Statement stmt = con.createStatement();
ResultSet rs=stmt.executeQuery("SELECT user, email FROM usuarios WHERE id="+userId+";");
rs.next();
String user=rs.getString("user");
String email=rs.getString("email");
rs.close();
stmt.close();
connMgr.freeConnection("mysql",con);
application.log("- Liberada la conexion con la BBDD");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
  <title>GPSou</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <script type="text/JavaScript">
<!--
var confirmarCambios=false;

function envia() {
        if (document.form1.newPasswd.value=="") {
                alert('<fmt:message key="msg.1"/>');
		document.form1.newPasswd.focus();
		return false;
        } else if (document.form1.rNewPasswd.value=="") {
                alert('<fmt:message key="msg.2"/>');
		document.form1.rNewPasswd.focus();
		return false;
        } else if (document.form1.email.value=="") {
                alert('<fmt:message key="msg.3"/>');
		document.form1.email.focus();
		return false;
        } else if (document.form1.newPasswd.value!=document.form1.rNewPasswd.value) {
                alert('<fmt:message key="msg.4"/>');
		document.form1.newPasswd.focus();
		return false;
        } else if (document.form1.email.value.match("(^[0-9a-zA-Z]+(?:[._][0-9a-zA-Z]+)*)@([0-9a-zA-Z]+(?:[._-][0-9a-zA-Z]+)*\\.[0-9a-zA-Z]{2,3})$")==null) {
                alert('<fmt:message key="msg.5"/>');
                document.form1.email.focus();
                return false;
        } else {
		if(!confirmarCambios) {
			document.getElementById("claveActual").style.display="block";
			document.form1.oldPasswd.focus();
			confirmarCambios=true;
			return false;
		} else if(document.form1.oldPasswd.value=="") {
                	alert('<fmt:message key="msg.6"/>');
			document.form1.oldPasswd.focus();
			return false;
		} else
			return true;
	}
}

//-->
  </script>

<style type="text/css">
  a {color: black}
</style>
</head>
<body onLoad='document.getElementById("claveActual").style.display="none";'>
  <p style="text-align: center"><img src="/VEGPS/html/images/GPSou-logo-banner.png" alt="GPS-o&ugrave;"></p>

<div style="text-align: center">
<p>
<br>
<b><fmt:message key="data.text"/></b>
<br>
<br>
</p>
<form name="form1" action="/VEGPS/ServletGoogleGPS" method="post" onsubmit="return envia()"> 
  <p><input type="hidden" name="p" value="cambiarDatos"></p>
  <table style="margin-left: auto; margin-right: auto">
      <tr align="right">
		<td valign="top"><fmt:message key="data.user"/></td>
		<td valign="top" align="left"><a href="/VEGPS/jsp/CambiarNombre.jsp" title="<fmt:message key="data.user.title"/>"><%=user%></a></td>
	 </tr>
	 <tr align="right">
		<td valign="top"><fmt:message key="data.passwd"/> </td>
		<td valign="top" align="left"><input type="password" title="<fmt:message key="data.passwd.title"/>" name="newPasswd" size="12" maxlength="12" onChange="document.form1.rNewPasswd.focus();"> </td>
	 </tr>
	 <tr align="right">
		<td valign="top"><fmt:message key="data.rpasswd"/> </td>
		<td valign="top" align="left"><input type="password" title="<fmt:message key="data.rpasswd.title"/>" name="rNewPasswd" size="12" maxlength="12" onchange="document.form1.email.focus();"> </td>
	 </tr>
	 <tr align="right">
		<td valign="top"><fmt:message key="data.email"/> </td>
		<td valign="top" align="left"><input type="text" title="<fmt:message key="data.email.title"/>" name="email" size="50" maxlength="255" value="<%=email%>"> </td>
	 </tr>
	 <tr align="center">
		<td> </td>
		<td valign="top" align="left"><input type="button" title="<fmt:message key="button.modify.title"/>" value="<fmt:message key="button.modify"/>" onClick='envia();'></td>
	 </tr>
	
  </table>
  <div id="claveActual">
    <br>
    <br>
    <fmt:message key="data.currPasswd"/> <input type="password" title="<fmt:message key="data.currPasswd.title"/>" name="oldPasswd" size="12" maxlength="12"><br>
    <input type="submit" title="<fmt:message key="button.ok.title"/>" value="<fmt:message key="button.ok"/>">
  </div>
  </form>
  <p>
  <br>
  <a href="/VEGPS/jsp/Principal.jsp" title="<fmt:message key="menu.title"/>"><fmt:message key="menu"/></a>
  </p>
  <p>
    <img src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01 Strict" height="31" width="88">
  </p>
</div>
</body>
</html>
<%
}
%>
